package com.microHR.hr.controller.system.basic;

import com.microHR.hr.api.RespBean;
import com.microHR.hr.mapper.DepartmentMapper;
import com.microHR.hr.model.Department;
import com.microHR.hr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }
    @PostMapping("/")
    public RespBean addDep(@RequestBody Department dep)  {
        departmentService.addDep(dep);
        if (dep.getResult() == 1) {
            RespBean<Department> success = RespBean.success(dep);
            success.setMessage("添加成功");
            return success;
        }
        return RespBean.failed("添加失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteDepById(@PathVariable Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDepById(dep);
        if (dep.getResult() == -2) {
            return RespBean.failed("该部门下有子部门，删除失败");
        } else if (dep.getResult() == -1) {
            return RespBean.failed("该部门下有员工，删除失败");
        } else if (dep.getResult() == 1) {
            return RespBean.success("删除成功");
        }
        return RespBean.failed("删除失败");
    }
}
